//common functions
jQuery.extend(jQuery.fx, {
    tick: function () {
        var timer,
    			timers = jQuery.timers,
    			i = 0;

        for (; i < timers.length; i++) {
            timer = timers[i];
            // Checks the timer has not already been removed
            if (!timer() && timers[i] === timer) {
                timers.splice(i--, 1);
            }
        }

        if (!timers.length) {
            jQuery.fx.stop();
        }
    }
});

jQuery.fn.extend({    
    serializeFromData: function (prefix) {
        var val = new Object();
        var attrs = this[0].attributes;
        for (var i = 0; i < attrs.length; i++) {
            if (attrs[i].name.indexOf(prefix) == 0) {
                val[attrs[i].name.replace(prefix, "")] = attrs[i].value;
            }
        }
        return val;
    }
});


function extendClass(child, parent) {
    var F = function () { }
    F.prototype = parent.prototype
    child.prototype = new F()
    child.prototype.constructor = child
    child.superclass = parent.prototype
}

function executeFunctionByName(functionName, context /*, args */) {
    var args = Array.prototype.slice.call(arguments).splice(2);
    var namespaces = functionName.split(".");
    var func = namespaces.pop();
    for (var i = 0; i < namespaces.length; i++) {
        context = context[namespaces[i]];
    }
    return context[func].apply(this, args);
}

if (typeof String.prototype.startsWith != 'function') {
    String.prototype.startsWith = function (str) {
        return this.slice(0, str.length) == str;
    };
}

if (typeof String.prototype.trim != 'function') {
    String.prototype.trim = function () {
        return this.replace(/^\s+|\s+$/g, "");
    };
}

//fix for Telerik editor image browser
var jQueryDataFn = $.fn.data;
$.fn.data = function (key, value) {
    if (key) {
        if (key === "thumbUrl") {
            key = key.toLowerCase();
        }
    }

    return jQueryDataFn.call(this, key, value);
};

//common structures
function cmsAdminStructures() {
}

function cmsAdminExpandItem(isExpanded, actionUrl, parent, childrenSelector, container) {
    this.isExpanded = isExpanded;
    this.actionUrl = actionUrl;
    this.parent = parent;
    this.childrenSelector = childrenSelector;
    this.container = container;
}

extendClass(cmsAdminExpandItem, cmsAdminStructures);

//options
$.cmsAdminOptions = function (options) {
    $.cmsAdminOptions.options = $.extend($.cmsAdminOptions.options, options);
    return this;
};

$.cmsAdminOptions.options = {
    fileUploaderOptions: {},
    fileViewOpions: {},
    knownUrls: {}
}

$.cmsAdminOptions.options.fileUploaderOptions = {
    supportedRuntimes: "",
    uploadUrl: "",
    maxFileSize: "",
    chunkSize: "",
    imageTypeExtensions: "",
    videoTypeExtensions: "",
    docTypeExtensions: "",
    archiveTypeExtensions: ""
};

$.cmsAdminOptions.options.fileViewOpions = {
    templateViewUrl: "",
    pageViewUrl: "",
    selectFileViewEvent: 'selected',
    uploadPreviewHeight: 0,
    uploadPreviewWidth: 0
};

$.cmsAdminOptions.options.knownUrls = {
    newMediaFolderUrl: "",
    createNewMediaFolderUrl: "",
    mediaPreviewPath: "",
    previewMediaUrl: "",
    allMediaContentTagsUrl: "",
    createMediaContentUrl: "",
    editPageUrl: "",
    viewPageUrl: "",
    workflowViewPageUrl: "",
    previewPageUrl: "",
    pageControlsUrl: "",
    pageTreeHistoryUrl: "",
    pageControlAddPartialViewUrl: "",
    pageControlAddActionViewUrl: "",
    pageControlSaveUrl: "",
    pageAnnotationsUrl: "",
    switchSiteUrl: "",
    startWorkflowMultiUrl: "",
    deleteMultiUrl: "",
    withdrawMultiUrl: "",
    siteWorkflowPanelUrl: "",
    pageIndexUrl : ""
};

//events
$.cmsAdminEvents = function (events) {
    $.cmsAdminEvents.registeredEvents = $.extend($.cmsAdminEvents.registeredEvents, events);
    return this;
};

$.cmsAdminEvents.registeredEvents = {
    ClearUserChangePasswordForm: "ClearUserChangePasswordForm",
    UpdateImages: "UpdateImages",
    ShowHideChildren: "ShowHideChildren",
    PageModuleCreated: "PageModuleCreated"
};




